﻿using System;
using System.Globalization;
using System.IO;

namespace Ue.Components.FileResources
{
    public class FileResourceManager : System.Resources.ResourceManager
    {
        private string baseName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="directory">资源文件所在的目录</param>
        /// <param name="resourceFileBaseName">资源文件名（排除扩展名的部分）</param>
        /// <param name="fileExtension">资源文件的扩展名</param>
        public FileResourceManager(string directory,string baseName,string extension)
        {
            this.Directory = directory;
            this.Extension = extension.TrimStart('.');

            this.baseName = baseName;
        }

        /// <summary>
        /// 资源文件目录
        /// </summary>
        public string Directory { get; private set; }

        /// <summary>
        /// 资源文件扩展名
        /// </summary>
        public string Extension { get; private set; }

        #region 重载方法

        protected override string GetResourceFileName(CultureInfo culture)
        {
            var fileName = string.Format(
                "{1}\\{0}.{2}", 
                this.baseName, 
                culture, 
                this.Extension);
            
            var path = Path.Combine(this.Directory, fileName); 
            if (File.Exists(path))  
            {    
                return path;     
            }
            
            return Path.Combine(this.Directory, string.Format("{0}.{1}", this.baseName, this.Extension));     
        }

        #endregion
    }
}
